﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Data;
using JXT.PrimaryKey.Batman.Extensions;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using JXT.PrimaryKey.Batman.Domain.VirtualExchange.Models;
using JXT.PrimaryKey.Batman.Domain.VirtualExchange.Repositories;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql;
using JXT.PrimaryKey.Batman.Domain.Repositories.MySql.Expressions;
using JXT.PrimaryKey.Batman.Domain.Quotation.Models;
using JXT.PrimaryKey.Batman.Domain.Repositories;

namespace JXT.PrimaryKey.Batman.Repositories.VirtualExchange
{
    public class MySqlUserTransactionDetailRepository : MySqlRepositoryBase<UserTransactionDetail, int>, IUserTransactionDetailRepository
    {
        public MySqlUserTransactionDetailRepository(RepositoryContext context)
            : base((MySqlRepositoryContext)context)
        {
        }

        public IEnumerable<UserTransactionDetail> GetListByProfit(IPagedSpecification<UserTransactionDetail> pagedSpecification)
        {
            return GetList(pagedSpecification);
        }

        public IEnumerable<UserTransactionDetail> GetListByTurnover(IPagedSpecification<UserTransactionDetail> pagedSpecification)
        {
            return GetList(pagedSpecification);
        }

        public UserTransactionDetail GetSingleByUserId(IPagedSpecification<UserTransactionDetail> pagedSpecification)
        {
            var result = GetSingle(pagedSpecification);
            return result;
        }

        public DateTime? GetLastDateTime(RankCountType rankCountType = RankCountType.Unknown)
        {
            if (rankCountType == RankCountType.Unknown)
                rankCountType = RankCountType.Day;
            string sql = " select max(`updatetime`) as updatetime from `usertransactiondetail` where `counttype`=@counttype";
            var parameters = new[] { Context.CreateParameter("@counttype", (int)rankCountType) };
            DateTime? result = null;
            using (var dr = Context.ExecuteDataReader(sql, parameters))
            {
                if (dr.Read())
                {
                    object obj = dr["updatetime"];
                    if (obj != DBNull.Value && obj != null)
                        result = Convert.ToDateTime(obj);
                }
            }
            return result;
        }

        public UserTransactionDetail GetLastSingle(int userId, int typeId, DateTime time, RankCountType type)
        {
            string sql = "select * from `usertransactiondetail` where `userid`=@userId and `typeId`=@typeId and `updatetime`<@dt and `counttype`=@rankCountType order by `updatetime` desc limit 1";
            var parameters = new[] 
            {
                Context.CreateParameter("@userId", userId),
                Context.CreateParameter("@typeId", typeId),
                Context.CreateParameter("@dt", time.Date),
                Context.CreateParameter("@rankCountType", (int)type)
            };
            UserTransactionDetail detail = null;
            using (var dr = Context.ExecuteDataReader(sql, parameters))
            {
                if (dr.Read())
                {
                    detail = ReadEntityFromDataReader(dr);
                }
            }
            return detail;
        }

        public UserTransactionDetail GetCurrentOrLastSingle(int userId, int typeId, DateTime time, RankCountType type)
        {
            string sql = "select * from `usertransactiondetail` where `userid`=@userId and `typeId`=@typeId and `updatetime`<=@dt and `counttype`=@rankCountType order by `updatetime` desc limit 1";
            var parameters = new[] 
            {
                Context.CreateParameter("@userId", userId),
                Context.CreateParameter("@typeId", typeId),
                Context.CreateParameter("@dt", time.Date),
                Context.CreateParameter("@rankCountType", (int)type)
            };
            UserTransactionDetail detail = null;
            using (var dr = Context.ExecuteDataReader(sql, parameters))
            {
                if (dr.Read())
                {
                    detail = ReadEntityFromDataReader(dr);
                }
            }
            return detail;
        }
    }
}
